package com.cadre.common.utils.oldutil;

import com.cadre.common.exception.CadreException;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/* loaded from: WordReplaceUtil.class */
public class WordReplaceUtil {
    public static void downloadWord(Map<String, String> params, String inPath, String outPath, HttpServletResponse response) {
        try {
            XWPFDocument doc = generateWord(params, inPath);
            generateWord(params, inPath);
            toDownload(doc, outPath, response);
        } catch (Exception e) {
            System.out.println("Error");
            e.printStackTrace();
        }
    }

    public static String getFileSufix(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    public static void replaceFooterAndHeader(XWPFDocument doc, Map<String, String> params) {
        List<XWPFParagraph> footers = doc.getHeaderFooterPolicy().getDefaultFooter().getParagraphs();
        List<XWPFParagraph> headers = doc.getHeaderFooterPolicy().getDefaultHeader().getParagraphs();
        for (XWPFParagraph paragraph : footers) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                if (StringUtils.isNotEmpty(text)) {
                    for (Map.Entry<String, String> entry : params.entrySet()) {
                        String key = entry.getKey();
                        if (text.indexOf(key) != -1) {
                            Object value = entry.getValue();
                            if (value instanceof String) {
                                text = text.replace(key, value.toString());
                                run.setText(text, 0);
                            }
                        }
                    }
                }
            }
        }
        for (XWPFParagraph paragraph2 : headers) {
            for (XWPFRun run2 : paragraph2.getRuns()) {
                String text2 = run2.getText(0);
                if (StringUtils.isNotEmpty(text2)) {
                    for (Map.Entry<String, String> entry2 : params.entrySet()) {
                        String key2 = entry2.getKey();
                        if (text2.indexOf(key2) != -1) {
                            Object value2 = entry2.getValue();
                            if (value2 instanceof String) {
                                text2 = text2.replace(key2, value2.toString());
                                run2.setText(text2, 0);
                            }
                        }
                    }
                }
            }
        }
    }

    public static XWPFDocument generateWord(Map<String, String> param, String filePath) {
        XWPFDocument doc = null;
        try {
            doc = new XWPFDocument(new ClassPathResource(filePath).getInputStream());
            if (param != null && param.size() > 0) {
                processParagraphs(doc.getParagraphs(), param, doc);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return doc;
    }

    public static void processParagraphs(List<XWPFParagraph> paragraphList, Map<String, String> param, XWPFDocument doc) {
        if (paragraphList != null && paragraphList.size() > 0) {
            for (XWPFParagraph paragraph : paragraphList) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String text = run.getText(0);
                    if (text != null) {
                        for (Map.Entry<String, String> entry : param.entrySet()) {
                            String key = entry.getKey();
                            if (text.indexOf(key) != -1) {
                                Object value = entry.getValue();
                                if (value instanceof String) {
                                    text = text.replace(key, value.toString());
                                    run.setText(text, 0);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void toDownload(XWPFDocument document, String fileName, HttpServletResponse response) {
        try {
            response.setContentType("application/octet-stream");
            response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes(), "ISO8859-1") + "\"");
            OutputStream out = response.getOutputStream();
            document.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            throw new CadreException(ResultEnum.OFFICE_ERROR);
        }
    }
}
